var app = angular.module("myapp1", []);
app.controller("ctrl1", function($scope) {

    {
        $.ajax({
            url: "/loadTask",
            type: "GET",
            // data: "",
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.todos);
                if (data) {
                    $scope.todos = data.todos;
                    $scope.doneTodos = data.doneTodos;
                    storelist();
                }
            },
            error: function () {
            }
        })
        localOnload();
    }
    //联网初始化数据



    //本地初始化同步数据
    function localOnload(){
        //初始化同步数据
            if (store.get("todo")) {
                // console.log(JSON.parse(store.get("todo")));
                var obj = JSON.parse(store.get("todo"));
                var arr = [];
                for (var i in obj) {
                    arr.push({ text: obj[i].text, isDone: obj[i].isDone });
                    // console.log(obj[i]);
                }
                $scope.todos = arr
                // console.log($scope.todos)
            } else {
                $scope.todos = [];
            }

            if (store.get("done")) {
                // 初始化localstore doneTodos
                var obj2 = JSON.parse(store.get("done"));
                var arr2 = [];
                for (var i in obj2) {
                    arr2.push({ text: obj2[i].text, isDone: obj2[i].isDone });
                    // console.log(obj[i]);
                }
                $scope.doneTodos = arr2;
            } else {
                $scope.doneTodos = [];
            }
    }



    function checklist() {
        var arr_todo = $scope.todos;
        var arr_done = $scope.doneTodos;
        for (var i in arr_todo) {
            if (arr_todo[i].text === $scope.List) {
                alert("重复输入");
                return false;
            } else {
                for (var j in arr_todo) {
                    if (arr_done.text === $scope.List) {
                        alert("输入重复");
                        return false;
                    }
                }
            }
        }
        return true;
    }


    //ajax请求 实现同步到服务器功能 通过session
    function  sendPhPbyAjax() {
        $.ajax({
            url:"/onloadTask",
            type:"get",
            data:{datas: {"todos":$scope.todos, "doneTodos": $scope.doneTodos}},
            success:function (data) {
                if(data===0){
                    console.log("账户没有登录");
                    $("#hintLogin").fadeIn();
                }
                if(data === 1){
                    console.log("数据已经给更新");
                    $("#hintLogin").fadeOut();
                }
                if(data === 2){
                    console.log("不是一个ajax请求");
                }
            },
            error:function (err) {
                console.log(err);
            }
        });
    }

    function storelist() {
        if (!window.localStorage) {
            alert("浏览器不支持localstorage");
            return false;
        } else {
            store.set("todo", JSON.stringify($scope.todos))
            store.set("done", JSON.stringify($scope.doneTodos))
        }
        sendPhPbyAjax();
    }



    //添加代办
    $scope.addlist = function() {
        if (String($scope.List) === "@clear") {
            store.clear();
            $scope.todos = [];
            $scope.doneTodos = [];
            storelist();
            alert("初始化成功");
            $scope.List = "";
        } else if ($scope.List.length > 0 && checklist()) {
            // console.log($scope.List);
            $scope.todos.push({ text: $scope.List, isDone: false })
            storelist();
            $scope.List = "";
            // console.log($scope.todos);
            // $scope.todos
        }
    }

    //删除
    $scope.delete = function(todoList, key) {
        // document.getElementById(todoList[key].text).setAttribute("class", "list-group-item animated bounceOutDown")
        // $("#" + todoList[key].text).remove();
        // setTimeout(function() {
        //     location.reload();
        // }, 300)
        todoList.splice(key, 1)[0];
        storelist();
        // console.log(todoList);

    }

    //完成代办
    $scope.done = function(key) {
        var willDone = $scope.todos.splice(key, 1)[0]
        willDone.isDone = true;
        $scope.doneTodos.push(willDone);
        storelist();
    }

    //撤回代办
    $scope.undo = function(key) {
        var backTodo = $scope.doneTodos.splice(key, 1)[0];
        backTodo.isDone = false;
        $scope.todos.push(backTodo);
        storelist();
    }




})